<template>
    <button 
    class="layui-btn <% if(size){ %> layui-btn-<%= size %> <% } %><% if(color){ %> layui-btn-<%= color %> <% } %>"
    <% if(formId && buttonType == "submit"){ %> lay-submit lay-filter="form_submit_<%= formId %>" <% } else 
    if(butId){ %> id="button_id_<%= butId %>" <% } %>
    > <% if(icon){ %> <i class="layui-icon <%= icon %>"></i> <% } %> <%= buttonText %></button>
</template>
<script name="compile">
    function templateCompile(data) {
        var defaultData = {
            type: "module",
            size: false,
            color: false,
            icon: false,
            templet: false,
            children: false,
            formId:false,
            butId:false
        };
        var newData = Object.assign(defaultData, data);
        var callback = {
            success: true,
            result: newData
        };
        if (newData.formId || newData.butId) {
            if (newData.formId && !newData.buttonType) {
                return {
                    success: false,
                    result: "表单按钮模块有参数formId参数下，必须传按钮类型参数：buttonType"
                };
            }
        }else{
            return {
                success: false,
                result: "按钮模块必须传参数：formId或butId"
            };
        }
        if (!newData.buttonText) {
            return {
                success: false,
                result: "表单按钮模块缺少参数：buttonText"
            };
        }
        if (!(newData.children || newData.templet)) {
            return {
                success: false,
                result: "表单参数：buttonList数组缺少参数：children或templet"
            };
        }
        
        return callback;
    }
</script>
<script name="templateScript">
    <% if(formId){ %>
    //监听提交
    form.on('submit(form_submit_<%= formId %>)', function (obj) {
        console.log(obj);
        var data = obj.field;
        <% if (templet) { %>
            <%- templet %>
        <% } %>
        <% if (children) { %>
            //&&scriptchildren&&
        <% } %>
        return false;
    });
    <% }else if(butId){ %>
        $("body").on("click","#button_id_<%= butId %>",function(){
            <% if (templet) { %>
                <%- templet %>
            <% } %>
            <% if (children) { %>
                //&&scriptchildren&&
            <% } %>
        });
    <% } %>
</script>